package com.zhida.smartcontrol.pagehome.deviceinfo;

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.jay.tools.utils.SharedUtil;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.Keys;
import com.zhida.smartcontrol.common.bean.DeviceClassifyBean;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;
import com.zhida.smartcontrol.pagectrl.ProjectListBean;
import com.zhida.smartcontrol.pagectrl.projectdetail.GroupBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: tanjianpei
 * date: 2019/10/9 009 15:39
 * description:
 */
public class DeviceInfoPresenter extends BasePresenterImpl<DeviceInfoContract.View> implements DeviceInfoContract.Presenter {
    private DeviceClassifyBean classifyBean;
    private String code;
    private String address;
    private String lat;
    private String lng;
    private String equipmentCode;
    private String num;
    private String name;
    private String imei;
    private String factory;
    private String groupId;
    private DeviceBean bean;
    private String mPic = "";
    private String pid;
    private String group_id;
    private String projectBaseId;
    private int pluginVersion;
    private String transformerRatio;
    private String iccid;

    @Override
    public void findByQrCode(String imei, String equipmentCode) {
        Params params = Params.newBuilder()
                .add("imei", imei)
                .build();
        mKalleCenter.get(Api.FIND_BY_IMEI, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                if (TextUtils.isEmpty(result)) {
                    mView.unInstallDevice(imei, equipmentCode);
                    return;
                }
                DeviceBean bean = JSON.parseObject(result, DeviceBean.class);
                mView.showBean(bean);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
                mView.unInstallDevice(imei, equipmentCode);
            }
        });
    }

    @Override
    public void requestKzq(String groupId, String pid) {
        if (TextUtils.isEmpty(groupId)) {
            return;
        }
        Params params = Params.newBuilder()
                .add("switchType", "0")
                .add("groupId", groupId)
                .build();
        mKalleCenter.get(Api.DEVICE_LIST, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<LampBean> lampBeans = JSON.parseArray(result, LampBean.class);
                for (int i = 0; i < lampBeans.size(); i++) {
                    if (lampBeans.get(i).getId().equals(pid)) {
                        mView.showKzq(lampBeans.get(i));
                    }
                }
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void requestProject(String projectBaseId) {
        Params params = Params.newBuilder()
                .add("projectName", "")
                .build();
        mKalleCenter.get(Api.PROJECT_LIST, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                SharedUtil.putString(Keys.PROJECT_LIST, result);
                parseProjectList(result, projectBaseId);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });

    }

    private void parseProjectList(String json, String projectBaseId) {
        List<ProjectListBean> projectList = JSON.parseArray(json, ProjectListBean.class);
        for (int i = 0; i < projectList.size(); i++) {
            if (projectList.get(i).getId().equals(projectBaseId)) {
                mView.showProject(projectList.get(i));
            }
        }
    }


    @Override
    public void requestGroup(String projectId, String groupId) {
        Params params = Params.newBuilder()
                .add("projectBaseId", projectId)
                .build();
        mKalleCenter.get(Api.GROUP_LIST, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<GroupBean> groupList = JSON.parseArray(result, GroupBean.class);
                for (int i = 0; i < groupList.size(); i++) {
                    if (groupList.get(i).getId().equals(groupId)) {
                        mView.showGroup(groupList.get(i));
                    }
                }
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

//    @Override
//    public void addFirst(DeviceClassifyBean classifyBean,String name,String imei,String equipmentCode,String manufacturer,String pluginVersion,String transformerRatio,String iccid,String num) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("switchType", classifyBean.getId());
//        params.put("equipmentCode", equipmentCode);
//        params.put("num", num);
//        params.put("name", name);
//        params.put("imei", imei);
//        params.put("pluginVersion", pluginVersion);
//        params.put("manufacturer", manufacturer);
//        params.put("transformerRatio", transformerRatio);
//        params.put("iccid", iccid);
//        mKalleCenter.postJson(Api.SAVE_LAMP_SWITCH_TEMP, params, true, new KalleCallBack() {
//            @Override
//            public void onSuccess(String result, String msg) {
//                mView.showMsg(msg);
//            }
//
//            @Override
//            public void onFailed(String msg) {
//                mView.showMsg(msg);
//            }
//        });
//    }

    public void check(DeviceClassifyBean classifyBean, String address, String lat, String lng,
                      String equipmentCode, String num, String name, String imei,
                      String factory, String groupId, DeviceBean bean, String filePath, int pluginVersion, String transformerRatio, String iccid, String pid, String group_id, String projectBaseId) {
        if (null == classifyBean || TextUtils.isEmpty(classifyBean.getId())) {
            mView.showMsg("请选择设备类型");
            return;
        }
        if (TextUtils.isEmpty(imei)) {
            mView.showMsg("请选择IMEI码");
            return;
        }
        if (null != this.bean && TextUtils.isEmpty(groupId)) {
            mView.showMsg("请选择区段");
            return;
        }
        if (null != this.bean && TextUtils.isEmpty(address)) {
            mView.showMsg("请输入地址");
            return;
        }
        if (null != this.bean && (TextUtils.isEmpty(lng) || TextUtils.isEmpty(lat))) {
            mView.showMsg("请选择经纬度");
            return;
        }
        if (TextUtils.isEmpty(equipmentCode)) {
            mView.showMsg("请输入设备码");
            return;
        }
        if (TextUtils.isEmpty(num)) {
            mView.showMsg("请输入编号");
            return;
        }
        if (TextUtils.isEmpty(name)) {
            mView.showMsg("请输入名称");
            return;
        }
        if (TextUtils.isEmpty(factory)) {
            mView.showMsg("请输入生产厂家");
            return;
        }

        this.classifyBean = classifyBean;
        this.address = address;
        this.lat = lat;
        this.lng = lng;
        this.equipmentCode = equipmentCode;
        this.num = num;
        this.name = name;
        this.imei = imei;
        this.factory = factory;
        this.groupId = groupId;
        this.bean = bean;
        this.group_id = group_id;
        this.projectBaseId = projectBaseId;
        this.pid = pid;
        this.pluginVersion = pluginVersion;
        this.transformerRatio = transformerRatio;
        this.iccid = iccid;

        if (TextUtils.isEmpty(filePath)) {
            submit();
        } else {
            upLoad(filePath);
        }

    }

    private void upLoad(String filePath) {
        mKalleCenter.upLoad(Api.UPLOAD_PIC, filePath, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mPic = result;
                submit();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    private void submit() {
        if (null == this.bean) { // 新添加设备
            int type = Integer.parseInt(this.classifyBean.getId());
            if (type == Const.NB_SINGLE || type == Const.NB_DOUBLE || type == Const.ZIG_BEE_SINGLE || type == Const.PLC_SINGLE) {
                if (type == Const.ZIG_BEE_SINGLE || type == Const.PLC_SINGLE) {
                    this.code = "";
                } else {
                    this.code = "";
                }
                this.pid = "";//所属集控器的id
                addLamp(); //添加单双灯
            } else {
                addDevice(); //添加集控，二代开关
            }
        } else { // 编辑设备
            editDevice();
        }
    }

    private void editDevice() {
        Map<String, Object> params = new HashMap<>();
        params.put("address", this.address);
        params.put("lng", this.lng);
        params.put("lat", this.lat);
        params.put("equipmentCode", this.equipmentCode);
        params.put("num", this.num);
        params.put("name", this.name);
        params.put("id", this.bean.getId());
        params.put("groupsId", group_id);
        params.put("pid", pid);
        params.put("projectBaseId", projectBaseId);
        params.put("manufacturer", this.factory);
        params.put("devicePic", this.mPic);
        params.put("pluginVersion", this.pluginVersion);
        params.put("transformerRatio", this.transformerRatio);
        params.put("iccid", this.iccid);
        mKalleCenter.putJson(Api.DEVICE_EDIT, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    private void addLamp() {
        // 添加单/双灯
        Map<String, Object> params = new HashMap<>();
        params.put("switchType", this.classifyBean.getId());
        params.put("code", this.code);
        params.put("address", this.address);
        params.put("equipmentCode", this.equipmentCode);
        params.put("num", this.num);
        params.put("name", this.name);
        params.put("imei", this.imei);
        params.put("pid", this.pid);
        params.put("groupsId", group_id);
        params.put("projectBaseId", projectBaseId);
        params.put("manufacturer", this.factory);
        params.put("devicePic", mPic);
        params.put("lng", this.lng);
        params.put("lat", this.lat);
        params.put("pluginVersion", this.pluginVersion);
        params.put("transformerRatio", this.transformerRatio);
        params.put("iccid", this.iccid);
        mKalleCenter.postJson(Api.ADD_LAMP, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    private void addDevice() {
        // 添加集控或二代开关
        Map<String, Object> params = new HashMap<>();
        params.put("switchType", this.classifyBean.getId());
        params.put("address", this.address);
        params.put("equipmentCode", this.equipmentCode);
        params.put("num", this.num);
        params.put("name", this.name);
        params.put("imei", this.imei);
        params.put("manufacturer", this.factory);
        params.put("devicePic", mPic);
        params.put("lng", this.lng);
        params.put("lat", this.lat);
        params.put("pid", pid);
        params.put("projectBaseId", projectBaseId);
        params.put("groupsId", this.groupId);
        params.put("pluginVersion", this.pluginVersion);
        params.put("transformerRatio", this.transformerRatio);
        params.put("iccid", this.iccid);
        mKalleCenter.postJson(Api.DEVICE_ADD, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
